package com.game.business.service.impl;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.game.business.domain.TOrder;
import com.game.business.domain.TWallet;
import com.game.business.service.ITWalletService;
import com.game.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.game.business.mapper.TCzOrderMapper;
import com.game.business.domain.TCzOrder;
import com.game.business.service.ITCzOrderService;

/**
 * 充值订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-19
 */
@Service
public class TCzOrderServiceImpl extends ServiceImpl<TCzOrderMapper,TCzOrder>  implements ITCzOrderService
{
    @Autowired
    private TCzOrderMapper tCzOrderMapper;

    /**
     * 查询充值订单
     *
     * @param id 充值订单主键
     * @return 充值订单
     */
    @Override
    public TCzOrder selectTCzOrderById(String id)
    {
        return tCzOrderMapper.selectTCzOrderById(id);
    }

    /**
     * 查询充值订单列表
     *
     * @param tCzOrder 充值订单
     * @return 充值订单
     */
    @Override
    public List<TCzOrder> selectTCzOrderList(TCzOrder tCzOrder)
    {
        return tCzOrderMapper.selectTCzOrderList(tCzOrder);
    }

    /**
     * 新增充值订单
     *
     * @param tCzOrder 充值订单
     * @return 结果
     */
    @Override
    public int insertTCzOrder(TCzOrder tCzOrder)
    {
                tCzOrder.setCreateTime(DateUtils.getNowDate());
            return tCzOrderMapper.insertTCzOrder(tCzOrder);
    }

    /**
     * 修改充值订单
     *
     * @param tCzOrder 充值订单
     * @return 结果
     */
    @Override
    public int updateTCzOrder(TCzOrder tCzOrder)
    {
                tCzOrder.setUpdateTime(DateUtils.getNowDate());
        return tCzOrderMapper.updateTCzOrder(tCzOrder);
    }

    /**
     * 批量删除充值订单
     *
     * @param ids 需要删除的充值订单主键
     * @return 结果
     */
    @Override
    public int deleteTCzOrderByIds(String[] ids)
    {
        return tCzOrderMapper.deleteTCzOrderByIds(ids);
    }

    /**
     * 删除充值订单信息
     *
     * @param id 充值订单主键
     * @return 结果
     */
    @Override
    public int deleteTCzOrderById(String id)
    {
        return tCzOrderMapper.deleteTCzOrderById(id);
    }

    @Override
    public TCzOrder getOrderByOrderNo(String orderNo) {
        LambdaQueryWrapper<TCzOrder> orderquery = new LambdaQueryWrapper<TCzOrder>().eq(TCzOrder::getOrderNo, orderNo);
        TCzOrder order = this.getOne(orderquery);
        return order;

    }

    @Autowired
    private ITWalletService walletService;


    @Override
    public void processOrder(String orderNo) {
        String uuId = IdUtil.simpleUUID();
        //处理重复的通知
        TCzOrder tOrder = this.getOrderByOrderNo(orderNo);
        //接口调用的幂等性：无论接口被调用多少次，产生的结果是一致的。
        String orderStatus = tOrder.getOrderStatus();
        if (!"1".equals(orderStatus)) {
            return;
        }
        //更新订单状态
        tOrder.setOrderStatus("2");
        tOrder.setPayTime(DateUtil.date());
        this.updateById(tOrder);
        TWallet tWallet = new TWallet();
        tWallet.setUserId(tOrder.getUserId());
        tWallet.setMoney(tOrder.getJzbMoney());
        // 充值
        tWallet.setType("10");
        tWallet.setBusinessId(tOrder.getId());
        tWallet.setRemark("充值成功");
        walletService.save(tWallet);
    }


}
